package com.cml.sqlversionupgradestarter.config;

import com.cml.sqlversionupgradestarter.context.SQLVersionUpgradeContext;
import com.cml.sqlversionupgradestarter.service.SQLVersionUpgradeManager;
import com.cml.sqlversionupgradestarter.verif.EnvVerification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * 作者：陈茂林
 * 时间：2022-1-6
 * 描述：sql版本升级启动类
 */
@Configuration
public class SqlVersionUpgradeAutoConfiguration {
    private Logger logger= LoggerFactory.getLogger(SqlVersionUpgradeAutoConfiguration.class);
    @Autowired
    private DataSource dataSource;

    @Value("${db.upgrade.time}")
    private String dbUpgradeTime;
    @PostConstruct
    public void start(){
        logger.info("sql版本开始初始化");
        if(EnvVerification.verif()){
            SQLVersionUpgradeContext sqlVersionUpgradeContext =new SQLVersionUpgradeContext.Builder()
                    .createSqlVersionUpgradeService(dbUpgradeTime)
                    .createJdbcConnManager(this.dataSource)
                    .build();
            SQLVersionUpgradeManager sqlVersionUpgradeManager=new SQLVersionUpgradeManager(sqlVersionUpgradeContext);
            sqlVersionUpgradeManager.upgrade();
        }
        logger.info("sql版本初始化结束");
    }
}
